package com.topic.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Random;

public class EmitLogTopicSimple {

    private static final String EXCHANGE_NAME = ReceiveLogsTopicSimple.EXCHANGE_NAME;

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            //注意最后一个消息是可以发到队列中去的，*号在这里当成一个普通的字符，虽然在绑定key有特殊含义
            String[] keys = {"quick.orange.rabbit",
                    "lazy.orange.elephant",
                    "quick.orange.fox",
                    "lazy.brown.fox",
                    "lazy.pink.rabbit",
                    "quick.brown.fox",
                    "orange",
                    "quick.orange.male.rabbit",
                    "lazy.orange.male.rabbit",
                    "*.orange.*"};
            for (int i = 0; i < 30; i++) {
                String routingKey = getRoutingKey(keys);
                String message = "123" + " seq：" + i;

                channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
                System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
            }
        }
    }

    private static String getRoutingKey(String[] keys) {
        Random random = new Random();
        int i = random.nextInt(keys.length);
        return keys[i];
    }


}